import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Main {
    static private int N_screen = 6; //计分屏（赛道）的个数
    static private int N_match = 9; //赛局数
    static private List<Screen> screens = new ArrayList<>(); //计分屏
    static private List<SetTeamName> setTeamNames = new ArrayList<>(); //设置赛道工具栏
    static private List<ScoreTable> scoreTables = new ArrayList<>(); //主窗口各赛道信息
    static private StorageManager storageManager = new StorageManager(); //存储管理器
    static private MQTT mqtt = new MQTT(screens,scoreTables,storageManager);

    static private void initWindows(){
        for(int i=1;i<=N_screen;i++){
            Screen screen = new Screen("计分屏"+i, N_match);
            SetTeamName setTeamName = new SetTeamName(i, screen);
            setTeamNames.add(setTeamName);
            screens.add(screen);
        }
    }

    static private JPanel initTop(){
        JPanel jPanel = new JPanel();
        final JLabel image = new JLabel(); //比赛图标
        image.setIcon(new ImageIcon("src/main/resources/icon.jpg"));
        jPanel.add(image);

        JPanel jPanel_top = new JPanel(); //头部（主体）
        jPanel_top.setLayout(new GridLayout(2,1,3,3));
        JPanel jPanel1 = new JPanel();
        JLabel jLabel_setMatchName = new JLabel("设置比赛名称：");
        final JTextField matchName_input = new JTextField(28);
        matchName_input.setText("第X届冰壶比赛");
        JButton jButton_matchName = new JButton("确定");
        jButton_matchName.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                for(int i=0;i<N_screen;i++)
                    screens.get(i).setMatchName(matchName_input.getText());
            }
        });
        for(int i=0;i<N_screen;i++)
            screens.get(i).setMatchName(matchName_input.getText());
        jPanel1.add(jLabel_setMatchName);
        jPanel1.add(matchName_input);
        jPanel1.add(jButton_matchName);
        JLabel jLabel_setPic = new JLabel("设置比赛赛标图");
        final JTextField Pic_input = new JTextField(28);
        JButton jButton_see = new JButton("浏览");
        jButton_see.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 打开文件选择窗口
                JFileChooser jfc = new JFileChooser();
                jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
                jfc.setFileFilter(new FileFilter() {
                    @Override
                    public boolean accept(File f) {
                        if (f.isDirectory())
                            return true;
                        String extension = f.getName().substring(f.getName().lastIndexOf(".")+1);
                        return (extension.equals("gif") || extension.equals("jpeg") || extension.equals("jpg") || extension.equals("png") ||
                                extension.equals("GIF") || extension.equals("JPEG") || extension.equals("JPG") || extension.equals("PNG"));
                    }

                    @Override
                    public String getDescription() {
                        return "图片文件(*.jpg, *.jpeg, *.gif, *.png)";
                    }
                });
                jfc.showDialog(new JLabel(), "选择");
                if(jfc.getSelectedFile()!=null)
                    Pic_input.setText(jfc.getSelectedFile().getAbsolutePath());
            }
        });
        JButton jButton_picPath = new JButton("确定");
        jButton_picPath.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                image.setIcon(new ImageIcon(Pic_input.getText()));
                for(int i=0;i<N_screen;i++)
                    screens.get(i).setPicPath(Pic_input.getText());
            }
        });
        jPanel1.add(jLabel_setPic);
        jPanel1.add(Pic_input);
        jPanel1.add(jButton_see);
        jPanel1.add(jButton_picPath);
        jPanel_top.add(jPanel1);
        JPanel jPanel2 = new JPanel();
        JButton[] button_screens = {new JButton(), new JButton(), new JButton(), new JButton(), new JButton(), new JButton()};
        int i = 0;
        for(final JButton button: button_screens){
            i++;
            button.setText("计分屏"+i);
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String num = button.getText().substring(button.getText().length()-1);
                    screens.get(Integer.parseInt(num)-1).showWindow();
                }
            });
            jPanel2.add(button);
        }
        JButton jButton_changeScore = new JButton("分数纠正");
        jButton_changeScore.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new ChangeScore(screens, scoreTables, N_match, storageManager);
            }
        });
        jPanel2.add(jButton_changeScore);
        JButton jButton_showTotal = new JButton("显示总分");
        jButton_showTotal.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new ShowTotal(screens,scoreTables);
            }
        });
        jPanel2.add(jButton_showTotal);
        JButton jButton_changeStyle = new JButton("显示设置");
        jButton_changeStyle.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new ChangeStyle(screens,scoreTables);
            }
        });
        jPanel2.add(jButton_changeStyle);
        JButton jButton_networks = new JButton("网络设置");
        jButton_networks.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Networks(mqtt);
            }
        });
        jPanel2.add(jButton_networks);
        jPanel_top.add(jPanel2);

        jPanel.add(jPanel_top);
        return jPanel;
    }

    public static void main(String[] args){
        initWindows();
        JFrame jFrame = new JFrame();
        jFrame.setLayout(new BorderLayout());

        jFrame.add(initTop(), BorderLayout.NORTH);

        JPanel jPanel_left = new JPanel(); //左侧设置区
        jPanel_left.setLayout(new GridLayout(N_screen,1));
        for(SetTeamName setTeamName:setTeamNames)
            jPanel_left.add(setTeamName);
        jFrame.add(jPanel_left,BorderLayout.WEST);

        JPanel jPanel_center = new JPanel(); //显示所有赛道的信息
        jPanel_center.setLayout(new GridLayout(N_screen,1));
        for(int j=0;j<N_screen;j++) {
            ScoreTable scoreTable = new ScoreTable(N_match);
            jPanel_center.add(scoreTable);
            scoreTables.add(scoreTable);
        }
        jFrame.add(jPanel_center,BorderLayout.CENTER);

        jFrame.add(new Monitor(),BorderLayout.EAST); //监控区

        jFrame.setTitle("裁判系统");
        jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        jFrame.setSize(1400,800);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
    }
}

class SetTeamName extends JPanel{
    private String group_name = "组别";
    private String team_red = "红队";
    private String team_yellow = "黄队";

    SetTeamName(int i, Screen screen){
        JPanel jPanel0 = new JPanel();
        JLabel jLabel0 = new JLabel("组别名称：");
        JTextField jTextField0 = new JTextField(10);
        jTextField0.setText(group_name);
        JButton jButton0 = new JButton("确定");
        jButton0.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                group_name = jTextField0.getText();
                screen.setGroupName(group_name);
            }
        });
        screen.setGroupName(group_name);
        jPanel0.add(jLabel0);
        jPanel0.add(jTextField0);
        jPanel0.add(jButton0);

        JPanel jPanel1 = new JPanel();
        JLabel jLabel1 = new JLabel("红队队名：");
        JTextField jTextField1 = new JTextField(10);
        jTextField1.setText(team_red);
        JButton jButton1 = new JButton("确定");
        jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                team_red = jTextField1.getText();
                screen.setTeam1Name(team_red);
            }
        });
        screen.setTeam1Name(team_red);
        jPanel1.add(jLabel1);
        jPanel1.add(jTextField1);
        jPanel1.add(jButton1);

        JPanel jPanel2 = new JPanel();
        JLabel jLabel2 = new JLabel("黄队队名：");
        JTextField jTextField2 = new JTextField(10);
        jTextField2.setText(team_yellow);
        JButton jButton2 = new JButton("确定");
        jButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                team_yellow = jTextField2.getText();
                screen.setTeam2Name(team_yellow);
            }
        });
        screen.setTeam2Name(team_yellow);
        jPanel2.add(jLabel2);
        jPanel2.add(jTextField2);
        jPanel2.add(jButton2);

        this.setLayout(new GridLayout(3,1));
        this.setBorder(new TitledBorder("赛道"+i));
        this.add(jPanel0);
        this.add(jPanel1);
        this.add(jPanel2);
    }

    String getGroup_name(){
        return group_name;
    }

    String getTeam_red(){
        return team_red;
    }

    String getTeam_yellow(){
        return team_yellow;
    }
}

class Monitor extends JPanel{
    Monitor(){
        this.setBorder(new TitledBorder("监控区"));
        this.add(new JLabel("这里显示实时监控画面"));
    }
}